//
//  NSDictionary+Extension.m
//  Demo
//
//  Created by XCY–hqF on 16/1/23.
//  Copyright © 2016年 XCY–hqF. All rights reserved.
//

#import "NSDictionary+Extension.h"

@implementation NSDictionary (Extension)

- (BOOL)isNotNull:(NSString *)key;
{
    if(![self objectForKey:key]){
        return NO;
    }
    
    id obj = [self objectForKey:key];// judge NSNull
    
    return ![obj isEqual:[NSNull null]];
}

- (NSString *)urlDictToStringWithUrlStr:(NSString *)urlStr
{
    if (!self) return urlStr;
        
    NSMutableArray *parts = [NSMutableArray array];
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        //接收key
        NSString *finalKey = [key stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        NSString *part =[NSString stringWithFormat:@"%@=%@",finalKey,obj];
        
        [parts addObject:part];
        
    }];
    //处理中文和空格问题
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //拼接URL
    NSString *queryString = [parts componentsJoinedByString:@"&"];
    queryString = queryString ? [NSString stringWithFormat:@"?%@",queryString] : @"";
    NSString *pathStr = [NSString stringWithFormat:@"%@%@",urlStr, queryString];
    return pathStr;
}

@end
